<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
      *  后台用户添加页面
      *  /admin/user/add
      */
    public function getAdd() {
        return view('d.user.add');
    }

    /**
      *  后台用户执行添加操作
      *  /admin/user/insert
      */
    public function postInsert(Request $request) {
        
        $this -> validate($request,[

            // 设置检验规则
            'username' => 'required',
            'password' => 'required',
            'repassword' => 'required|same:password',
            'email' => 'required|email',
        ],[

            // 设置报错信息
            'username.required' => '账号不能为空',
            'password.required' => '密码不能为空',
            'repassword.required' =>'确认密码不能为空',
            'email.required' => '邮箱不能为空',
            'email.email' => '邮箱格式不正确',
        ]);

        $data = $request -> except(['_token','repassword']);
        $data['token'] = str_random(50);
        $data['password'] = Hash::make($data['password']);
        $res = DB::table('user') -> insert($data);
        if($res) {
            return redirect('/admin/user/index');
        } else {
            return back() -> with('error','添加失败');
        }
    }

    /**
     *  后台用户浏览页面
     *  /admin/user/index
     */
    public function getIndex(Request $request) {

        // 获取分页数据
        $page = $request -> has('num') ? $request -> only('num')['num'] : '5';

        // 获取数据
        $list = DB::table('user') -> where(function($query)use($request) {
            if($request -> has('keywords')) {
                    $keywords = $request -> input('keywords');
                    $query -> where('username','like','%'.$keywords.'%')
                    ->orwhere('email','like','%'.$keywords.'%');
                }   
        }) -> paginate($page);
        return view('d.user.index',['list' => $list,'page' => $page,'request' => $request]);
    }

    /**
     *  后台用户执行删除
     *  /admin/user/delete
     */
    public function getDelete($id) {
        $res = DB::table('user') -> where('id','=',$id) -> delete();
        if ($res) {
            return redirect('/admin/user/index') -> with('success','删除成功');
        } else {
            return back() -> with('error','删除失败');
        }
    }

     /**
     *  后台用户编辑页面
     *  /admin/user/edit
     */
     public function getEdit($id) {
        $res = DB::table('user') -> where('id','=',$id) -> first();
        return view('d.user.edit',['v' => $res]);   
     }

     /**
     *  后台执行编辑操作
     *  /admin/user/edit
     */
     public function postUpdate(Request $request) {
        $data = $request -> except(['_token','id']);
        $res = DB::table('user') -> where('id','=',$request -> input('id')) -> update($data);
        if($res) {
            return redirect('/admin/user/index') -> with('success','编辑成功');
        } else {
            return back() -> with('error','编辑失败');
        }
     }
}
